const jwt = require('jsonwebtoken');
const jwt_koa = require('koa-jwt');
const {secret} = require('../configs/config');
const Token = {
    encrypt: function (payload = {}, time) { //data加密数据，time过期时间  生成jwt-token

        return jwt.sign(payload, secret, {expiresIn: time})
    },
    decrypt: function (token) { //解密jwt-token
        try {
            let data = jwt.verify(token.split(' ')[0], secret);
            return {
                token: true,
                data: data
            };
        } catch (e) {
            return {
                token: false,
                data: e
            }
        }
    },
    auth: function () {
        return jwt_koa({secret}) //验证token中间件
    }


};
module.exports = Token;
